﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DslShell = global::Microsoft.VisualStudio.Modeling.Shell;
using VSShell = global::Microsoft.VisualStudio.Shell;
using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;
using DslValidation = global::Microsoft.VisualStudio.Modeling.Validation;
using System.ComponentModel.Design;


namespace devtm.AutoMapper
{

    internal partial class AutoMapperCommandSet
	{

        

        protected override System.Collections.Generic.IList<System.ComponentModel.Design.MenuCommand> GetMenuCommands()
        {

            // Get the standard commands
            global::System.Collections.Generic.IList<global::System.ComponentModel.Design.MenuCommand> commands = base.GetMenuCommands();


            global::System.ComponentModel.Design.MenuCommand menuCommand
            = new DslShell::DynamicStatusMenuCommand(
                new global::System.EventHandler(GenerateFromStatus),
                new global::System.EventHandler(GenerateFrom),
                Constants.CreateRootMap);
            commands.Add(menuCommand);


            global::System.ComponentModel.Design.MenuCommand menuCommand2
            = new DslShell::DynamicStatusMenuCommand(
                new global::System.EventHandler(ShowDetailsStatus),
                new global::System.EventHandler(ShowDetails),
                Constants.ShowDetails);
            commands.Add(menuCommand2);


            return commands;

        }


        /// <summary>
        /// Handler for validating the model.
        /// </summary>
        internal virtual void GenerateFromStatus(object sender, System.EventArgs e)
        {

            if (this.CurrentAutoMapperDocData != null && this.CurrentAutoMapperDocData.Store != null)
                this.CurrentAutoMapperDocData.ValidationController.Validate(this.CurrentAutoMapperDocData.GetAllElementsForValidation(), DslValidation::ValidationCategories.Menu);
            
        }

        /// <summary>
        /// Handler for validating the model.
        /// </summary>
        internal virtual void ShowDetailsStatus(object sender, System.EventArgs e)
        {

            if (this.CurrentAutoMapperDocData != null && this.CurrentAutoMapperDocData.Store != null)
                this.CurrentAutoMapperDocData.ValidationController.Validate(this.CurrentAutoMapperDocData.GetAllElementsForValidation(), DslValidation::ValidationCategories.Menu);

        }


        internal virtual void GenerateFrom(object sender, System.EventArgs e)
        {

            if (this.CurrentAutoMapperDocData != null && this.CurrentAutoMapperDocData.Store != null)

                foreach (object selectedObject in this.CurrentSelection)
                {
                    // Build list of elements embedded beneath the selected root.
                    DslModeling::ModelElement element = GetValidationTarget(selectedObject);
                    if (element != null && element is MappingModel)
                    {
                        (element as MappingModel).CreateMapper();
                        break;
                    }
                }
        }


        internal virtual void ShowDetails(object sender, System.EventArgs e)
        {

            AutomapperDetailsToolWindows details = this.AutoMapperViewDetails;

            if (details != null)            
                details.Show();
            
        }


        /// <summary>
        /// Returns the explorer tool window.
        /// </summary>
        protected AutomapperDetailsToolWindows AutoMapperViewDetails
        {
            get
            {

                AutomapperDetailsToolWindows explorerWindow = null;
                DslShell::ModelingPackage package = this.ServiceProvider.GetService(typeof(VSShell::Package)) as DslShell::ModelingPackage;

                if (package != null)                
                    explorerWindow = package.GetToolWindow(typeof(AutomapperDetailsToolWindows), true) as AutomapperDetailsToolWindows;
                
                return explorerWindow;
            }
        }
   

    }

}
